package com.tti.kiosofthercules;


import android.support.test.espresso.ViewInteraction;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;
import android.test.suitebuilder.annotation.LargeTest;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;

import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.click;
import static android.support.test.espresso.action.ViewActions.closeSoftKeyboard;
import static android.support.test.espresso.action.ViewActions.replaceText;
import static android.support.test.espresso.action.ViewActions.scrollTo;
import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withParent;
import static android.support.test.espresso.matcher.ViewMatchers.withText;
import static com.tti.kiosofthercules.TestUtils.sleep;
import static org.hamcrest.Matchers.allOf;

@LargeTest
@RunWith(AndroidJUnit4.class)
public class RegistrationFailTest {

    @Rule
    public ActivityTestRule<SrcActivity> mActivityTestRule = new ActivityTestRule<>(SrcActivity.class);

    @Test
    public void registrationFailTest() {
        ViewInteraction appCompatButton = onView(
                allOf(withId(R.id.reg_start), withText("Scan QR Code"), isDisplayed()));
        appCompatButton.perform(click());

        sleep(10000);

        ViewInteraction appCompatButton2 = onView(
                allOf(withId(R.id.signin_reg_btn), withText("Register IT'S FREE!"), isDisplayed()));
        appCompatButton2.perform(click());

        ViewInteraction appCompatEditText = onView(
                withId(R.id.reg_email));
        appCompatEditText.perform(scrollTo(), click());

        ViewInteraction appCompatEditText2 = onView(
                withId(R.id.reg_email));
        appCompatEditText2.perform(scrollTo(), replaceText("asdf"), closeSoftKeyboard());

        ViewInteraction appCompatEditText3 = onView(
                withId(R.id.reg_pwrd));
        appCompatEditText3.perform(scrollTo(), replaceText("111111"), closeSoftKeyboard());

        ViewInteraction appCompatEditText4 = onView(
                withId(R.id.reg_pwrd_confirm));
        appCompatEditText4.perform(scrollTo(), replaceText("111111"), closeSoftKeyboard());

        ViewInteraction appCompatTextView = onView(
                allOf(withId(R.id.save_btn), withText("Save"), isDisplayed()));
        appCompatTextView.perform(click());

        ViewInteraction textView = onView(
                allOf(withId(android.R.id.message), withText("Email is not Valid"),
                        childAtPosition(
                                childAtPosition(
                                        withId(R.id.scrollView),
                                        0),
                                0),
                        isDisplayed()));
        textView.check(matches(withText("Email is not Valid")));

        ViewInteraction appCompatButton3 = onView(
                allOf(withId(android.R.id.button1), withText("OK"),
                        withParent(allOf(withId(R.id.buttonPanel),
                                withParent(withId(R.id.parentPanel)))),
                        isDisplayed()));
        appCompatButton3.perform(click());

        ViewInteraction appCompatEditText5 = onView(
                allOf(withId(R.id.reg_email)));
        appCompatEditText5.perform(scrollTo(), replaceText("bb@bb.com"), closeSoftKeyboard());

        ViewInteraction appCompatTextView2 = onView(
                allOf(withId(R.id.save_btn), withText("Save"), isDisplayed()));
        appCompatTextView2.perform(click());

        sleep(3000);

        ViewInteraction textView2 = onView(
                allOf(withId(android.R.id.message), withText("Email is already used by another user. Please choose another email."),
                        childAtPosition(
                                childAtPosition(
                                        withId(R.id.scrollView),
                                        0),
                                0),
                        isDisplayed()));
        textView2.check(matches(withText("Email is already used by another user. Please choose another email.")));

        ViewInteraction appCompatButton4 = onView(
                allOf(withId(android.R.id.button1), withText("OK"),
                        withParent(allOf(withId(R.id.buttonPanel),
                                withParent(withId(R.id.parentPanel)))),
                        isDisplayed()));
        appCompatButton4.perform(click());

        ViewInteraction appCompatEditText6 = onView(
                allOf(withId(R.id.reg_email)));
        appCompatEditText6.perform(scrollTo(), click());

        ViewInteraction appCompatEditText7 = onView(
                allOf(withId(R.id.reg_email)));
        appCompatEditText7.perform(scrollTo(), replaceText("ffff@ff.com"), closeSoftKeyboard());

        ViewInteraction appCompatEditText8 = onView(
                allOf(withId(R.id.reg_pwrd_confirm)));
        appCompatEditText8.perform(scrollTo(), replaceText("11111"), closeSoftKeyboard());

        ViewInteraction appCompatTextView3 = onView(
                allOf(withId(R.id.save_btn), isDisplayed()));
        appCompatTextView3.perform(click());

        ViewInteraction textView3 = onView(
                allOf(withId(android.R.id.message), withText("Password confirmation doesn't match Password"),
                        childAtPosition(
                                childAtPosition(
                                        withId(R.id.scrollView),
                                        0),
                                0),
                        isDisplayed()));
        textView3.check(matches(withText("Password confirmation doesn't match Password")));

        ViewInteraction appCompatButton5 = onView(
                allOf(withId(android.R.id.button1), withText("OK"),
                        withParent(allOf(withId(R.id.buttonPanel),
                                withParent(withId(R.id.parentPanel)))),
                        isDisplayed()));
        appCompatButton5.perform(click());

        ViewInteraction appCompatEditText9 = onView(
                allOf(withId(R.id.reg_pwrd)));
        appCompatEditText9.perform(scrollTo(), replaceText("11111"), closeSoftKeyboard());

        ViewInteraction appCompatTextView4 = onView(
                allOf(withId(R.id.save_btn), isDisplayed()));
        appCompatTextView4.perform(click());

        ViewInteraction textView4 = onView(
                allOf(withId(android.R.id.message), withText("Password must be 6 to 16 alphanumeric characters"),
                        childAtPosition(
                                childAtPosition(
                                        withId(R.id.scrollView),
                                        0),
                                0),
                        isDisplayed()));
        textView4.check(matches(withText("Password must be 6 to 16 alphanumeric characters")));

        ViewInteraction appCompatButton6 = onView(
                allOf(withId(android.R.id.button1), withText("OK"),
                        withParent(allOf(withId(R.id.buttonPanel),
                                withParent(withId(R.id.parentPanel)))),
                        isDisplayed()));
        appCompatButton6.perform(click());

    }

    private static Matcher<View> childAtPosition(
            final Matcher<View> parentMatcher, final int position) {

        return new TypeSafeMatcher<View>() {
            @Override
            public void describeTo(Description description) {
                description.appendText("Child at position " + position + " in parent ");
                parentMatcher.describeTo(description);
            }

            @Override
            public boolean matchesSafely(View view) {
                ViewParent parent = view.getParent();
                return parent instanceof ViewGroup && parentMatcher.matches(parent)
                        && view.equals(((ViewGroup) parent).getChildAt(position));
            }
        };
    }
}
